package com.turbo.cloud.ods.web.logic;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.turbo.cloud.core.base.PageQuery;
import com.turbo.cloud.core.exception.BusinessException;
import com.turbo.cloud.core.security.utils.SecurityUtil;
import com.turbo.cloud.core.utils.PageUtil;
import com.turbo.cloud.ods.constants.OdsConstants;
import com.turbo.cloud.ods.web.entity.OdsReportSetting;
import com.turbo.cloud.ods.web.model.OdsReportSettingDo;
import com.turbo.cloud.ods.web.model.OdsReportSettingQo;
import com.turbo.cloud.ods.web.model.OdsReportSettingVo;
import com.turbo.cloud.ods.web.service.OdsReportSettingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 * 报表配置表 前端控制器
 * </p>
 *
 * @author zhangluning
 * @since 2024-08-07
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class OdsReportSettingLogic {

    final OdsReportSettingService odsReportSettingService;

    /**
     * 根据当前系统ID，查询报表配置分页列表
     *
     * @return Page
     */
    public Page<OdsReportSettingVo> queryReportSettingPage(PageQuery<OdsReportSettingQo> pageQuery) {
        OdsReportSettingQo odsReportSettingQo = PageUtil.getPageQueryParam(pageQuery, OdsReportSettingQo::new);

        LambdaQueryWrapper<OdsReportSetting> reportSettingLQW = new LambdaQueryWrapper<>();
        reportSettingLQW.eq(OdsReportSetting::getSystemId, SecurityUtil.getCurrentSystemId());

        reportSettingLQW.like(CharSequenceUtil.isNotBlank(odsReportSettingQo.getReportNameByLike()),
                OdsReportSetting::getReportName,
                odsReportSettingQo.getReportNameByLike());
        reportSettingLQW.like(CharSequenceUtil.isNotBlank(odsReportSettingQo.getReportCodeByLike()),
                OdsReportSetting::getReportCode,
                odsReportSettingQo.getReportCodeByLike());

        reportSettingLQW.orderByDesc(OdsReportSetting::getCreatedDate);

        Page<OdsReportSetting> odsReportSettingPage = odsReportSettingService.page(pageQuery.toPage(), reportSettingLQW);

        return PageUtil.toPageView(odsReportSettingPage, OdsReportSettingVo::new);
    }

    /**
     * 根据ID查询报表配置
     *
     * @return Page
     */
    public OdsReportSettingVo queryReportSettingById(String id) {
        OdsReportSetting odsReportSetting = odsReportSettingService.getById(id);

        OdsReportSettingVo odsReportSettingVo = new OdsReportSettingVo();
        BeanUtils.copyProperties(odsReportSetting, odsReportSettingVo);

        return odsReportSettingVo;
    }

    /**
     * 新增报表配置
     *
     * @param umsOdsReportSettingDo 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean createReportSetting(OdsReportSettingDo umsOdsReportSettingDo) {
        umsOdsReportSettingDo.setSystemId(SecurityUtil.getCurrentSystemId());

        LambdaQueryWrapper<OdsReportSetting> odsReportSettingLambdaQueryWrapper = new LambdaQueryWrapper<>();
        odsReportSettingLambdaQueryWrapper.eq(OdsReportSetting::getReportCode, umsOdsReportSettingDo.getReportCode());
        OdsReportSetting umsOdsReportSettingTmp = odsReportSettingService.getOne(odsReportSettingLambdaQueryWrapper);
        if (Objects.nonNull(umsOdsReportSettingTmp)) {
            throw new BusinessException("报表配置编码已经存在，请更换");
        }

        OdsReportSetting umsOdsReportSetting = new OdsReportSetting();
        BeanUtils.copyProperties(umsOdsReportSettingDo, umsOdsReportSetting);

        boolean odsReportSettingSaveResult = odsReportSettingService.save(umsOdsReportSetting);
        if (!odsReportSettingSaveResult) {
            throw new BusinessException("新增报表配置失败");
        }
        return true;
    }

    /**
     * 修改报表配置
     *
     * @param umsOdsReportSettingDo 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifyReportSetting(OdsReportSettingDo umsOdsReportSettingDo) {

        OdsReportSetting umsOdsReportSettingTmp = odsReportSettingService.getById(umsOdsReportSettingDo.getId());

        if (Objects.nonNull(umsOdsReportSettingTmp) && !umsOdsReportSettingTmp.getReportCode().equals(umsOdsReportSettingDo.getReportCode())) {
            LambdaQueryWrapper<OdsReportSetting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OdsReportSetting::getReportCode, umsOdsReportSettingDo.getReportCode());
            OdsReportSetting odsReportSetting = odsReportSettingService.getOne(lambdaQueryWrapper);
            if (Objects.nonNull(odsReportSetting)) {
                throw new BusinessException("报表配置编码已经存在，请更换");
            }
        }

        OdsReportSetting umsOdsReportSetting = new OdsReportSetting();
        BeanUtils.copyProperties(umsOdsReportSettingDo, umsOdsReportSetting);

        boolean result = odsReportSettingService.updateById(umsOdsReportSetting);
        if (!result) {
            throw new BusinessException("修改报表配置失败");
        }

        return true;
    }

    /**
     * 开启、关闭报表配置有效状态
     *
     * @param id 报表配置ID
     */
    public Boolean enabledReportSettingStateById(String id) {
        OdsReportSetting umsOdsReportSetting = odsReportSettingService.getById(id);
        if (Objects.isNull(umsOdsReportSetting)) {
            throw new BusinessException("报表配置不存在");
        }

        if (OdsConstants.DICT_STATE_ID_YES.equals(umsOdsReportSetting.getStateId())) {
            umsOdsReportSetting.setStateId(0);
        } else {
            umsOdsReportSetting.setStateId(1);
        }

        boolean result = odsReportSettingService.updateById(umsOdsReportSetting);
        if (!result) {
            throw new BusinessException("修改报表配置状态失败");
        }
        return true;
    }

    /**
     * 删除报表配置
     *
     * @param id 报表配置ID
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean removeReportSettingById(String id) {
        if (StrUtil.isBlank(id)) {
            throw new BusinessException("报表配置ID不能为空");
        }

        return odsReportSettingService.removeById(id);
    }
}
